package com.shxc.smarttrade.old.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.RedPack;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/16.
 */

public class RedPackAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    List<RedPack.DataBean.RedBean> redBeanList;
    private LayoutInflater mInflater;

    private ItemClickListener listener;

    public RedPackAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);

    }

    public void setDatas(List<RedPack.DataBean.RedBean> redBeanList) {
        this.redBeanList = redBeanList;
        notifyDataSetChanged();
    }

    public void setOnMyClickListener(ItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new RedPackHolder(mInflater.inflate(R.layout.adapter_red_pack, parent, false));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof RedPackHolder) {
            final RedPack.DataBean.RedBean rb = redBeanList.get(position);
            ((RedPackHolder) holder).tvMoney.setText(String.valueOf((int) rb.getAmt()));
            ((RedPackHolder) holder).tvRedPackName.setText(rb.getName());
            ((RedPackHolder) holder).tvStopDate.setText("截止日期：" + BaseUtils.getDateFromMillionSeconds(rb.getEndDate()));
            ((RedPackHolder) holder).tvDescribe.setText(rb.getDesc());
            int status = rb.getStatus();
            if (status == 1) {
                ((RedPackHolder) holder).tvStatus.setText("未完成");
                ((RedPackHolder) holder).tvStatus.setBackgroundResource(R.drawable.gray_text_bg);

                ((RedPackHolder) holder).tvStatus.setTextColor(mContext.getResources().getColor(R.color.main_text_color));

                ((RedPackHolder) holder).tvDollarSign.setTextColor(mContext.getResources().getColor(R.color.main_color));
                ((RedPackHolder) holder).tvMoney.setTextColor(mContext.getResources().getColor(R.color.main_color));
            } else if (status == 2) {
                ((RedPackHolder) holder).tvStatus.setText("可领取");
                ((RedPackHolder) holder).tvStatus.setBackgroundResource(R.drawable.yellow_text_bg);
                ((RedPackHolder) holder).tvDollarSign.setTextColor(mContext.getResources().getColor(R.color.main_color));
                ((RedPackHolder) holder).tvMoney.setTextColor(mContext.getResources().getColor(R.color.main_color));
                ((RedPackHolder) holder).tvStatus.setTextColor(mContext.getResources().getColor(R.color.main_color));
                ((RedPackHolder) holder).tvStatus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.setOnClickListener(rb);
                    }
                });
            } else if (status == 3) {
                ((RedPackHolder) holder).tvStatus.setText("已领取");
                ((RedPackHolder) holder).tvStatus.setBackgroundResource(R.drawable.gray_text_bg);
                ((RedPackHolder) holder).tvStatus.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                ((RedPackHolder) holder).tvMoney.setTextColor(mContext.getResources().getColor(R.color.secondary_text_color));
                ((RedPackHolder) holder).tvDollarSign.setTextColor(mContext.getResources().getColor(R.color.secondary_text_color));

            } else if (status == 4 || status == 5) {
                ((RedPackHolder) holder).tvStatus.setText("已过期");
                ((RedPackHolder) holder).tvStatus.setBackgroundResource(R.drawable.gray_text_bg);
                ((RedPackHolder) holder).tvStatus.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                ((RedPackHolder) holder).tvMoney.setTextColor(mContext.getResources().getColor(R.color.secondary_text_color));
                ((RedPackHolder) holder).tvDollarSign.setTextColor(mContext.getResources().getColor(R.color.secondary_text_color));
            }
        }

    }

    @Override
    public int getItemCount() {
        return redBeanList == null ? 0 : redBeanList.size();
    }


    public class RedPackHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_red_pack_name)
        TextView tvRedPackName;
        @BindView(R.id.tv_stop_date)
        TextView tvStopDate;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_describe)
        TextView tvDescribe;
        @BindView(R.id.tv_dollar_sign)
        TextView tvDollarSign;

        RedPackHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }

    public  interface ItemClickListener {
        void setOnClickListener(RedPack.DataBean.RedBean rb);
    }

}
